/*
 * AddNewUser.fx
 * Author: Yu Min-ting
 * Created on 2009-7-27, 20:36:13
 */

package com.sjtu.fx.admin;

import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.scene.layout.*;
import javafx.scene.shape.Line;

import com.sjtu.fx.tools.Panel;
import javafx.scene.control.*;

import javafx.stage.Alert;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.System;
import org.json.JSONObject;

import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;

import com.sjtu.fx.tools.UIAlert;


public class AddNewUser extends CustomNode{
    var panel:Panel;

    //user login name
    var lnameText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Login Name "
    };
    var lnameTextBox = TextBox {
        columns: 15
        blocksMouse:false
    }

    //user login password
    var pwText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Login Password "
    };
    var pwTextBox = TextBox {
        columns: 15
        blocksMouse:false
    }

    //user email
     var emailText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* Email-address "
    };
    var emailTextBox = TextBox {
        columns: 15
        blocksMouse:false
    }

    //user name
     var nameText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "* User Name "
    };
    var nameTextBox = TextBox {
        columns: 15
        blocksMouse:false
    }

    //user department
     var departText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Department "
    };
    var departTextBox = SwingComboBox {
            items: [
                SwingComboBoxItem {
                    text: "IDEAS"
                    selected: true
                }
            ]
        }

     var scoreText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "User Check in Score"
    };

    var scoreTextBox = SwingComboBox {
            items: [
                SwingComboBoxItem {
                    text: "0"
                    selected: true
                }SwingComboBoxItem {
                    text: "1"
                }SwingComboBoxItem {
                    text: "2"
                }SwingComboBoxItem {
                    text: "3"
                }SwingComboBoxItem {
                    text: "4"
                }SwingComboBoxItem {
                    text: "5"
                }SwingComboBoxItem {
                    text: "6"
                }SwingComboBoxItem {
                    text: "7"
                }SwingComboBoxItem {
                    text: "8"
                }SwingComboBoxItem {
                    text: "9"
                }SwingComboBoxItem {
                    text: "10"
                }
            ]
        }

     var required = Text {
             content: "* = Required"
             font: Font {size:11 name: "Verdana Bold"}
             fill: Color.RED
             translateX:40
             translateY:360
         }

     var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9

     }


     var completeButton = Button {
             text: "Complete"
             translateX:180 translateY:450
             action: function() {
                //not input the full textboxes
                if(lnameTextBox.text==""or pwTextBox.text==""
                     or emailTextBox.text=="" or nameTextBox.text=="")
                {
                 alert.visible = true;
                 //Alert.inform("ERROR","Please input entire information");
                }else{                     
                      sendRegisterInfo();
                      alert.title = "Success";
                      alert.content = "Add New User Successfully!";
                      alert.visible = true;
                      //Alert.inform("Success","Add New User Successfully!");
                      visible = false;
                      lnameTextBox.text = "";
                      pwTextBox.text ="";
                      emailTextBox.text = "";
                      nameTextBox.text = "";
                      }
                 
            }
    }

    override protected function create() : Node {

        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "Add New User"
                    translateX: 0
                    translateY: 0
                },
                VBox{
                    spacing: 30
                    translateX:40 translateY: 60
                    content:[lnameText, pwText, emailText]
                },
                 VBox{
                    spacing: 20
                    translateX:165 translateY: 55
                    content:[lnameTextBox, pwTextBox, emailTextBox]
                },
                Line {
                    startX: 25
                    startY: 180
                    endX: 405
                    endY: 180
                    stroke:Color.rgb(196,227,227)
                },
                VBox{
                    spacing: 30
                    translateX:40 translateY: 210
                    content:[nameText, departText, scoreText]
                },
                 VBox{
                    spacing: 20
                    translateX:165 translateY: 200
                    content:[nameTextBox, departTextBox, scoreTextBox]
                },
                 Line {
                    startX: 25
                    startY: 330
                    endX: 405
                    endY: 330
                    stroke:Color.rgb(196,227,227)
                },
                required,
                completeButton,
                alert

            ]
        }
   }

function sendRegisterInfo(){
    HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/Regist"
        onOutput: function(os:OutputStream) {
             try {
                var json:JSONObject = JSONObject{};
                json.put("username",lnameTextBox.text);
                json.put("password",pwTextBox.text);
                json.put("name",nameTextBox.text);
                json.put("email",emailTextBox.text);
                json.put("department",departTextBox.text);
                json.put("score",scoreTextBox.text);
                var temp:String = "obj={json.toString()}";
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }
        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
                var result:String = data.readLine();
                System.out.println(result);
            } finally {
                is.close();
            }
        }
    }.start();
}


}
